﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Nirvana
{
    /// <summary>
    /// UI栈管理器
    /// </summary>
    public class UIStackManager : MonoBehaviour
    {
        private List<UIWindowBase> m_normalStack = new List<UIWindowBase>();
        private List<UIWindowBase> m_fixedStack = new List<UIWindowBase>();
        private List<UIWindowBase> m_popupStack = new List<UIWindowBase>();
        private List<UIWindowBase> m_popupStack2 = new List<UIWindowBase>();
        private List<UIWindowBase> m_popupStack3 = new List<UIWindowBase>();
        private List<UIWindowBase> m_topBarStack = new List<UIWindowBase>();

        public void AddUIStack(UIWindowBase ui)
        {
            switch (ui.m_UIType)
            {
                case UIType.Fixed: m_fixedStack.Add(ui); break;
                case UIType.Normal: m_normalStack.Add(ui); break;
                case UIType.PopUp1: m_popupStack.Add(ui); break;
                case UIType.PopUp2: m_popupStack2.Add(ui); break;
                case UIType.PopUp3: m_popupStack3.Add(ui); break;
                case UIType.Top: m_topBarStack.Add(ui); break;
            }
        }

        public void RemoveUIStack(UIWindowBase ui)
        {
            switch (ui.m_UIType)
            {
                case UIType.Fixed: m_fixedStack.Remove(ui); break;
                case UIType.Normal: m_normalStack.Remove(ui); break;
                case UIType.PopUp1: m_popupStack.Remove(ui); break;
                case UIType.PopUp2: m_popupStack2.Remove(ui); break;
                case UIType.PopUp3: m_popupStack3.Remove(ui); break;
                case UIType.Top: m_topBarStack.Remove(ui); break;
            }
        }

        public void CloseLastUIWindow(UIType uiType = UIType.Normal)
        {
            var ui = GetLastUI(uiType);
            if (ui != null)
            {
                ui.OnClickClose();
            }
        }

        public UIWindowBase GetLastUI(UIType uiType)
        {
            UIWindowBase ui = null;
            switch (uiType)
            {
                case UIType.Fixed:
                    {
                        if (m_fixedStack.Count > 0) ui = m_fixedStack[m_fixedStack.Count - 1];
                        break;
                    }
                case UIType.Normal:
                    {
                        if (m_normalStack.Count > 0) ui = m_normalStack[m_normalStack.Count - 1];
                        break;
                    }
                case UIType.PopUp1:
                    {
                        if (m_popupStack.Count > 0) ui = m_popupStack[m_popupStack.Count - 1];
                        break;
                    }
                case UIType.PopUp2:
                    {
                        if (m_popupStack2.Count > 0) ui = m_popupStack2[m_popupStack2.Count - 1];
                        break;
                    }
                case UIType.PopUp3:
                    {
                        if (m_popupStack3.Count > 0) ui = m_popupStack3[m_popupStack3.Count - 1];
                        break;
                    }
                case UIType.Top:
                    {
                        if (m_topBarStack.Count > 0) ui = m_topBarStack[m_topBarStack.Count - 1];
                        break;
                    }

            }

            return ui;
        }
    }
}